package com.company.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.project.dao.*;
import com.company.project.model.*;
import com.company.project.model.VO.FilterVO;
import com.company.project.model.VO.StatisticsDetailDO;
import com.company.project.model.VO.StatisticsListVO;
import com.company.project.model.VO.ZhusuVO;
import com.company.project.service.VtZhusuService;
import com.company.project.core.AbstractService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * Created by CodeGenerator on 2023/10/17.
 */
@Service
@Transactional
public class VtZhusuServiceImpl extends AbstractService<VtZhusu> implements VtZhusuService {
    @Resource
    private VtZhusuMapper vtZhusuMapper;
    @Resource
    private VtProductMapper vtProductMapper;
    @Resource
    private VtMachineNumberMapper vtMachineNumberMapper;
    @Resource
    private VtManagerMapper vtManagerMapper;

    @Override
    public Integer getAccumulateNumber(VtZhusu vtZhusu) {
        VtZhusu zhusu = new VtZhusu();
        zhusu.setOrderId(vtZhusu.getOrderId());
        zhusu.setProductId(vtZhusu.getProductId());
        zhusu.setIsdel(0);
        List<VtZhusu> zhusuList = vtZhusuMapper.select(zhusu);
        Integer count = 0;
        for (VtZhusu temp:zhusuList) {
            count+= temp.getQualifiedQuantity();
        }
        return count;
    }

    @Override
    public List<ZhusuVO> findListByGroupId(VtZhusu vtZhusu) {
        List<VtZhusu> zhusuList = vtZhusuMapper.findListByGroupId(vtZhusu.getGroupid());
        List<ZhusuVO> zhusuVOS = new ArrayList<>();
        for (VtZhusu temp:zhusuList) {
            ZhusuVO zhusuVO = new ZhusuVO();
            zhusuVO.setOrderId(temp.getOrderId());
            VtProduct vtProduct = vtProductMapper.selectByPrimaryKey(temp.getProductId());
            zhusuVO.setProductName(vtProduct.getName());
            VtMachineNumber vtMachineNumber = vtMachineNumberMapper.selectByPrimaryKey(temp.getNumber());
            zhusuVO.setNumberName(vtMachineNumber.getName());
            VtManager vtManager = vtManagerMapper.selectByPrimaryKey(temp.getOperator());
            zhusuVO.setOperatorName(vtManager.getTruename());
            zhusuVO.setId(temp.getId());
            zhusuVO.setIsexamine(temp.getIsexamine());
            zhusuVOS.add(zhusuVO);
        }
        return zhusuVOS;
    }

    @Override
    public List<ZhusuVO> findTodayListByGroupId(long startTime, long endTime, Integer groupId, Integer isExamine) {
        List<VtZhusu> zhusuList = vtZhusuMapper.findTodayListByGroupId(startTime,endTime,groupId,isExamine);
        List<ZhusuVO> zhusuVOS = new ArrayList<>();
        for (VtZhusu temp:zhusuList) {
            ZhusuVO zhusuVO = new ZhusuVO();
            zhusuVO.setOrderId(temp.getOrderId());
            VtProduct vtProduct = vtProductMapper.selectByPrimaryKey(temp.getProductId());
            zhusuVO.setProductName(vtProduct.getName());
            VtMachineNumber vtMachineNumber = vtMachineNumberMapper.selectByPrimaryKey(temp.getNumber());
            zhusuVO.setNumberName(vtMachineNumber.getName());
            VtManager vtManager = vtManagerMapper.selectByPrimaryKey(temp.getOperator());
            zhusuVO.setOperatorName(vtManager.getTruename());
            zhusuVO.setId(temp.getId());
            zhusuVO.setIsexamine(temp.getIsexamine());
            zhusuVOS.add(zhusuVO);
        }
        return zhusuVOS;
    }

    @Override
    public List<ZhusuVO> findHistoryListByGroupId(long startTime, Integer groupId, Integer isExamine) {
        List<VtZhusu> zhusuList = vtZhusuMapper.findHistoryListByGroupId(startTime,groupId,isExamine);
        List<ZhusuVO> zhusuVOS = new ArrayList<>();
        for (VtZhusu temp:zhusuList) {
            ZhusuVO zhusuVO = new ZhusuVO();
            zhusuVO.setOrderId(temp.getOrderId());
            VtProduct vtProduct = vtProductMapper.selectByPrimaryKey(temp.getProductId());
            zhusuVO.setProductName(vtProduct.getName());
            VtMachineNumber vtMachineNumber = vtMachineNumberMapper.selectByPrimaryKey(temp.getNumber());
            zhusuVO.setNumberName(vtMachineNumber.getName());
            VtManager vtManager = vtManagerMapper.selectByPrimaryKey(temp.getOperator());
            zhusuVO.setOperatorName(vtManager.getTruename());
            zhusuVO.setId(temp.getId());
            zhusuVO.setIsexamine(temp.getIsexamine());
            zhusuVOS.add(zhusuVO);
        }
        return zhusuVOS;
    }

    @Override
    public List<ZhusuVO> findTodayAllList(long startTime, long endTime, Integer isExamine) {
        List<VtZhusu> zhusuList = vtZhusuMapper.findTodayAllList(isExamine);
        List<ZhusuVO> zhusuVOS = new ArrayList<>();
        for (VtZhusu temp:zhusuList) {
            ZhusuVO zhusuVO = new ZhusuVO();
            zhusuVO.setOrderId(temp.getOrderId());
            VtProduct vtProduct = vtProductMapper.selectByPrimaryKey(temp.getProductId());
            zhusuVO.setProductName(vtProduct.getName());
            VtMachineNumber vtMachineNumber = vtMachineNumberMapper.selectByPrimaryKey(temp.getNumber());
            zhusuVO.setNumberName(vtMachineNumber.getName());
            VtManager vtManager = vtManagerMapper.selectByPrimaryKey(temp.getOperator());
            zhusuVO.setOperatorName(vtManager.getTruename());
            zhusuVO.setId(temp.getId());
            zhusuVO.setIsexamine(temp.getIsexamine());
            zhusuVOS.add(zhusuVO);
        }
        return zhusuVOS;
    }

    @Override
    public List<ZhusuVO> findHistoryAllList(Integer isExamine, FilterVO filterVO) {
        if (Objects.isNull(filterVO)) {
            filterVO = new FilterVO();
            filterVO.setIsExamine(isExamine);
        }else  {
            filterVO.setIsExamine(isExamine);
        }
        List<VtZhusu> zhusuList = vtZhusuMapper.findHistoryAllList(filterVO);
        List<ZhusuVO> zhusuVOS = new ArrayList<>();
        for (VtZhusu temp:zhusuList) {
            ZhusuVO zhusuVO = new ZhusuVO();
            zhusuVO.setOrderId(temp.getOrderId());
            VtProduct vtProduct = vtProductMapper.selectByPrimaryKey(temp.getProductId());
            zhusuVO.setProductName(vtProduct.getName());
            VtMachineNumber vtMachineNumber = vtMachineNumberMapper.selectByPrimaryKey(temp.getNumber());
            zhusuVO.setNumberName(vtMachineNumber.getName());
            VtManager vtManager = vtManagerMapper.selectByPrimaryKey(temp.getOperator());
            zhusuVO.setOperatorName(vtManager.getTruename());
            zhusuVO.setId(temp.getId());
            zhusuVO.setIsexamine(temp.getIsexamine());
            zhusuVOS.add(zhusuVO);
        }
        return zhusuVOS;
    }

    @Override
    public List<StatisticsListVO> getStatisticsList() {
        LocalDate currentDate = LocalDate.now();
        // 设置当天的开始时间
        LocalDateTime startOfDay = currentDate.atStartOfDay();
        long startTimestamp = startOfDay.toEpochSecond(ZoneOffset.UTC);
//        startTimestamp += 28800;
        // 设置当天的结束时间
        LocalTime endOfDay = LocalTime.MAX;
        LocalDateTime endOfDate = LocalDateTime.of(currentDate, endOfDay);
        long endTimestamp = endOfDate.toEpochSecond(ZoneOffset.UTC);
//        endTimestamp += 28800;
        // 输出结果
        System.out.println("当天的开始时间戳：" + startTimestamp);
        System.out.println("当天的结束时间戳：" + endTimestamp);
        Date day=new Date();
        SimpleDateFormat sdf= new SimpleDateFormat("MM-dd");
        System.out.println(sdf.format(day));
        List<StatisticsListVO> statisticsListVOList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            StatisticsListVO statisticsListVO = new StatisticsListVO();
            statisticsListVO.setTodayDate(sdf.format(day));
            switch (i) {
                case 0:
                    statisticsListVO.setClasses(1);
                    statisticsListVO.setClassesName("白班");
                    statisticsListVO.setDirectorName("李强");
                    statisticsListVO.setGroupId(13);
                    statisticsListVO.setOperatorCount( vtZhusuMapper.getOperatorCount(startTimestamp,endTimestamp,13,1));
                    break;
                case 1:
                    statisticsListVO.setClasses(2);
                    statisticsListVO.setClassesName("夜班");
                    statisticsListVO.setDirectorName("李强");
                    statisticsListVO.setGroupId(13);
                    statisticsListVO.setOperatorCount( vtZhusuMapper.getOperatorCount(startTimestamp,endTimestamp,13,2));
                    break;
                case 2:
                    statisticsListVO.setClasses(1);
                    statisticsListVO.setClassesName("白班");
                    statisticsListVO.setDirectorName("王峰");
                    statisticsListVO.setGroupId(14);
                    statisticsListVO.setOperatorCount( vtZhusuMapper.getOperatorCount(startTimestamp,endTimestamp,14,1));
                    break;
                case 3:
                    statisticsListVO.setClasses(2);
                    statisticsListVO.setClassesName("夜班");
                    statisticsListVO.setDirectorName("王峰");
                    statisticsListVO.setGroupId(14);
                    statisticsListVO.setOperatorCount( vtZhusuMapper.getOperatorCount(startTimestamp,endTimestamp,14,2));
                    break;
                case 4:
                    statisticsListVO.setClasses(1);
                    statisticsListVO.setClassesName("白班");
                    statisticsListVO.setDirectorName("岳标");
                    statisticsListVO.setGroupId(15);
                    statisticsListVO.setOperatorCount( vtZhusuMapper.getOperatorCount(startTimestamp,endTimestamp,15,1));
                    break;
                case 5:
                    statisticsListVO.setClasses(2);
                    statisticsListVO.setClassesName("夜班");
                    statisticsListVO.setDirectorName("岳标");
                    statisticsListVO.setGroupId(15);
                    statisticsListVO.setOperatorCount( vtZhusuMapper.getOperatorCount(startTimestamp,endTimestamp,15,2));
                    break;
            };
            if (Objects.isNull(statisticsListVO.getOperatorCount()) || statisticsListVO.getOperatorCount() == 0 ) continue;
            statisticsListVOList.add(statisticsListVO);

        }
        return statisticsListVOList;
    }

    public List<StatisticsDetailDO> getStatisticsDetail(Integer groupId,Integer classes) {
        LocalDate currentDate = LocalDate.now();
        // 设置当天的开始时间
        LocalDateTime startOfDay = currentDate.atStartOfDay();
        long startTimestamp = startOfDay.toEpochSecond(ZoneOffset.UTC);
        // 设置当天的结束时间
        LocalTime endOfDay = LocalTime.MAX;
        LocalDateTime endOfDate = LocalDateTime.of(currentDate, endOfDay);
        long endTimestamp = endOfDate.toEpochSecond(ZoneOffset.UTC);
        // 输出结果
        startTimestamp += 28800;
        endTimestamp += 28800;
        System.out.println("当天的开始时间戳：" + startTimestamp);
        System.out.println("当天的结束时间戳：" + endTimestamp);
        Date day = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        System.out.println(sdf.format(day));
        return vtZhusuMapper.getStatisticsDetail(startTimestamp,endTimestamp,groupId,classes);
    }


}
